import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from functools import partial

# 使用pyuic5 CBJTest.ui -o CBJui.py生成
from CBJui import *
import inputAnalyze
import recorder


def checkInputData(ui):
    # check time
    date = ui.calendar.selectedDate().toString("yyMMdd")
    time = ui.lineEditTime.text()
    if time == '':
        time = recorder.getFmtTime()
    timer = recorder.CBJTimeRW()
    timer.checkDateTime(date, time)


def onBreakPoint(ui):
    try:
        checkInputData(ui)
        timer = recorder.CBJTimeRW()
        timer.setBreakPoint(endDate=ui.calendar.selectedDate().toString("yyMMdd"),
                            endTime=ui.lineEditTime.text(),
                            comment=ui.lineEditComment.text())
        onRefresh(ui)
    except Exception as e:
        print(e.args, e.with_traceback(None))


def onStart(ui):
    try:
        checkInputData(ui)
        timer = recorder.CBJTimeRW()
        timer.setLastStart(attributeNum=inputAnalyze.analyzeAttribute(ui),
                           startDate=ui.calendar.selectedDate().toString("yyMMdd"),
                           startTime=ui.lineEditTime.text(),
                           comment=ui.lineEditComment.text())
        onRefresh(ui)
    except Exception as e:
        print(e.args, e.__traceback__, e.__dict__)


def onEnd(ui):
    try:
        checkInputData(ui)
        timer = recorder.CBJTimeRW()
        timer.setLastEnd(attributeNum=inputAnalyze.analyzeAttribute(ui),
                         endDate=ui.calendar.selectedDate().toString("yyMMdd"),
                         endTime=ui.lineEditTime.text(),
                         comment=ui.lineEditComment.text())
        onRefresh(ui)
    except Exception as e:
        print(e.args, e.__traceback__)


def onRefresh(ui):
    try:
        timer = recorder.CBJTimeRW()
        lines = timer.readLastText()
        # print(lines)
        ui.textEdit.setText(''.join(lines))
    except Exception as e:
        print(e.args, e.__traceback__)


def onClear(ui: Ui_MainWindow):
    ui.lineEditTime.clear()


def onTest(ui: Ui_MainWindow):
    print(ui.textEdit.toPlainText())
    b = hex(inputAnalyze.analyzeAttribute(ui))[2:].upper()
    print(b)
    a = ui.calendar.selectedDate()
    print(a.toString("yyMMdd"))
    print(a)
    print(type(a))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    uiMainWindow = Ui_MainWindow()
    uiMainWindow.setupUi(MainWindow)
    MainWindow.show()

    # 添加配置
    # 传参, 可以传具体的参数, 也可以直接传ui
    uiMainWindow.buttonEnd.clicked.connect(partial(onEnd, uiMainWindow))
    uiMainWindow.buttonTest.clicked.connect(partial(onTest, uiMainWindow))
    uiMainWindow.buttonStart.clicked.connect(partial(onStart, uiMainWindow))
    uiMainWindow.buttonRefresh.clicked.connect(partial(onRefresh, uiMainWindow))
    uiMainWindow.buttonBreakPoint.clicked.connect(partial(onBreakPoint, uiMainWindow))
    uiMainWindow.buttonClear.clicked.connect(partial(onClear, uiMainWindow))
    onRefresh(uiMainWindow)
    sys.exit(app.exec_())
